package com.caffeinatedbliss.nanowrimo;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class PictureGraph implements DynamicImageGenerator {
  public static final Color BAR_INK = new Color(0xEB, 0x20, 0x20);
  public static final Color TEXT_INK = new Color(0x54, 0x30, 0x19);
  public static final Color DARK_INK_COLOR = new Color(0x1E, 0x7B, 0xAA);

  public static final int WIDTH = 120;
  public static final int HEIGHT = 90;

  ImageIcon icon = new ImageIcon(PictureGraph.class.getResource("2008-blank.png"));
  ImageIcon participantText = new ImageIcon(PictureGraph.class.getResource("2008-participant-text.png"));
  ImageIcon winnerText = new ImageIcon(PictureGraph.class.getResource("2008-winner-text.png"));
  ImageIcon[] nanoicon = new ImageIcon[]{
    new ImageIcon(PictureGraph.class.getResource("2008-coffee.png")),
    new ImageIcon(PictureGraph.class.getResource("2008-printout.png")),
    new ImageIcon(PictureGraph.class.getResource("2008-computer.png")),
    new ImageIcon(PictureGraph.class.getResource("2008-pens.png")),
  };


  private BufferedImage graphImage;
  private static final int BAR_TOP_POSITION = 79;

  public PictureGraph(String history, int goal) {
    NanowrimoComputer nanowrimoComputer = new NanowrimoComputer(history, goal);
    double percentComplete = clipPercentComplete(nanowrimoComputer.getPercentComplete());
    int current = clipCurrentAmount(nanowrimoComputer.getCurrent());
    init(percentComplete, current, "Participant");
  }

  public PictureGraph(int amount, int goal) {
    int current = clipCurrentAmount(amount);
    double percent = clipPercentComplete((100.0 * (double) amount) / (double) goal);
    String participant = (percent >= 100) ? "WINNER!" : "Participant";
    init(percent, current, participant);
  }

  public PictureGraph(String participant, int amount, int goal) {
    int current = clipCurrentAmount(amount);
    double percent = clipPercentComplete((100.0 * (double) amount) / (double) goal);
    if (participant == null) {
      participant = (percent >= 100) ? "WINNER!" : "Participant";
    }
    init(percent, current, participant);
  }

  private void init(double percentComplete, int current, String participant) {
    graphImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D) graphImage.getGraphics();

    icon.paintIcon(null, g, 0, 0);
    int nanoIconIndex = (int) ((System.currentTimeMillis() / 300000) % 4);
    nanoicon[nanoIconIndex].paintIcon(null, g, 0, 26);

    if (percentComplete >= 100.0) {
      winnerText.paintIcon(null, g, 45, 35);
    } else {
      participantText.paintIcon(null, g, 45, 35);
    }

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    Font boldFont = new Font("dialog", Font.PLAIN, 10);
    g.setFont(boldFont);
    FontMetrics fm = g.getFontMetrics(boldFont);
    int ascent = fm.getAscent();
    int descent = fm.getDescent();
    int totalLineHeght = ascent + descent;
    writeCenterText(g, fm, current + " words", totalLineHeght + 4);

    // g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    writeCenterText(g, fm, participant, totalLineHeght - 5);

    // g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);

    int width = (int) ((percentComplete / 100.0) * (WIDTH - 14));
    g.setColor(BAR_INK);
    g.fillRect(6, BAR_TOP_POSITION, width, 6);

    g.dispose();
  }

  private int clipCurrentAmount(int amount) {
    int current = amount;
    if (current < 0) {
      current = 0;
    }
    return current;
  }

  private double clipPercentComplete(double percentComplete) {
    if (percentComplete < 0) {
      percentComplete = 0;
    } else if (percentComplete > 100) {
      percentComplete = 100;
    }
    return percentComplete;
  }

  private void writeCenterText(Graphics g, FontMetrics fm, String theText, int yPosition) {
    int wordCountWidth = fm.stringWidth(theText);
    int leftEdgeWordCount = 60 - (wordCountWidth / 2);
    g.setColor(TEXT_INK);
    g.drawString(theText, leftEdgeWordCount, yPosition);
  }

  public BufferedImage getGraphImage() {
    return graphImage;
  }
}
